package and.activity;

import java.util.ArrayList;
import java.util.List;

import net.youmi.android.appoffers.YoumiOffersManager;
import net.youmi.android.appoffers.YoumiPointsManager;
import and.common.MyCommon;
import and.process.CreateNumProcess;
import and.process.QueryProcess;
import and.tools.AnimationHelper;
import and.tools.FileHelper;
import and.tools.ViewHelper;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class ChooseActivity extends BaseActivity {
	private CreateNumProcess createNumProcess = new CreateNumProcess();
	private ImageButton btnBack;
	private TextView tvHead;
	private TextView tvNum;
	private TextView tvDivider;
	private TextView tvDayLabel;
	private TextView tvRandomLabel;
	private LayoutParams paramsNum;
	private LayoutParams params;
	private Button btnYaojiang;
	private TextView tvYaojiang;
	private TextView tvYaojiangLabel;
	private LinearLayout ll1;
	private LinearLayout ll;
	private LinearLayout ll2;
	private ProgressDialog progressDialog;
	private Handler handler = new Handler();
	private int yaojiangCount;
	private String[] yaojiangNums;
	private int points = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setNowActivity(this);
		choose();
		points = YoumiPointsManager.queryPoints(this);
	}

	private void setContentByHandler() {
		handler.post(new Runnable() {
			public void run() {
				// 将获取到的数据更新到列表中
				setMyContent();
			}
		});
	}

	private void setMyContent() {
		String dayNum = createNumProcess.createTodayNum(MyCommon.QueryLength,
				MyCommon.QueryMaxNum);
		String[] dayNums = dayNum.split(" ");
		String randomNum = createNumProcess.createRandomUniqueNum(
				MyCommon.QueryLength, MyCommon.QueryMinNum,
				MyCommon.QueryMaxNum);
		String[] randomNums = randomNum.split(" ");

		params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		paramsNum = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, 1f);
		LayoutParams paramsWeight = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT, 1f);
		RelativeLayout.LayoutParams paramsBtn = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
		paramsBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		paramsBtn.height = 80;
		paramsBtn.width = 80;

		btnBack = new ImageButton(this);
		btnBack.setLayoutParams(paramsBtn);
		btnBack.setBackgroundResource(R.drawable.back);
		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					btnBack.setBackgroundResource(R.drawable.gray);
					BaseActivityGroup.changeActivity(ChooseActivity.this,
							"ChooseListActivity");
				} catch (Exception e) {
					System.out.println("返回出错");
					e.printStackTrace();
				}
			}
		});

		ScrollView sv = (ScrollView) this.getLayoutInflater().inflate(
				R.layout.scrollview, null);
		ll = (LinearLayout) sv.getChildAt(0);

		RelativeLayout headLl = new RelativeLayout(this);
		headLl.setLayoutParams(paramsWeight);
		ll1 = new LinearLayout(this);
		ll1.setLayoutParams(params);
		ll1.setOrientation(LinearLayout.HORIZONTAL);

		tvHead = new TextView(this);
		tvHead.setLayoutParams(params);
		tvHead.setText(MyCommon.QueryName);
		tvHead.setTextSize(26);
		tvDivider = ViewHelper.getDivider(this, 1);
		tvDayLabel = new TextView(this);
		tvDayLabel.setLayoutParams(params);
		tvDayLabel.setText("今日推荐：");
		tvDayLabel.setTextSize(20);

		ll.addView(headLl);
		headLl.addView(tvHead);
		headLl.addView(btnBack);
		ll.addView(tvDivider);

		createNums(dayNums, tvDayLabel);

		tvDivider = ViewHelper.getDivider(this, 1);
		ll.addView(tvDivider);
		tvRandomLabel = new TextView(this);
		tvRandomLabel.setLayoutParams(params);
		tvRandomLabel.setText("机选号码：");
		tvRandomLabel.setTextSize(20);
		createNums(randomNums, tvRandomLabel);

		btnYaojiang = new Button(this);
		btnYaojiang.setLayoutParams(paramsWeight);
		btnYaojiang.setTextSize(20);
		if (points == 0) {
			btnYaojiang.setText("获  取  积  分");
		} else {
			btnYaojiang.setText("开  始  摇  号(积分："+points+")");
		}
		if("test".equals(FileHelper
				.getText("http://android.snbw.cn/test.txt"))){
			btnYaojiang.setText("开  始  摇  号");
		}
		btnYaojiang.setBackgroundResource(R.drawable.btnselector);

		btnYaojiang.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if(!"test".equals(FileHelper
						.getText("http://android.snbw.cn/test.txt"))){
					if (points == 0) {
						YoumiOffersManager.showOffers(ChooseActivity.this,
								YoumiOffersManager.TYPE_REWARD_OFFERS);
						return;
					} else {
						YoumiPointsManager.spendPoints(ChooseActivity.this, 5);
						points = YoumiPointsManager
								.queryPoints(ChooseActivity.this);
						btnYaojiang.setText("开  始  摇  号(积分："+points+")");
					}
				}
				AnimationSet set = new AnimationSet(false);
				set.addAnimation(AnimationHelper.hide(1000));
				set.addAnimation(AnimationHelper.scaleIn(1000));
				set.setFillAfter(true);
				v.setAnimation(set);
				v.setEnabled(false);
				yaojiangCount = 0;
				String yaojiangNum = createNumProcess.createRandomUniqueNum(
						MyCommon.QueryLength, MyCommon.QueryMinNum,
						MyCommon.QueryMaxNum);
				yaojiangNums = yaojiangNum.split(" ");
				System.out.println(yaojiangNum);
				ll2 = new LinearLayout(ChooseActivity.this);
				ll2.setLayoutParams(params);
				ll2.setOrientation(LinearLayout.HORIZONTAL);
				tvYaojiangLabel = new TextView(ChooseActivity.this);
				tvYaojiangLabel.setLayoutParams(params);
				tvYaojiangLabel.setText("中奖号码：");
				tvYaojiangLabel.setTextSize(20);
				ll2.addView(tvYaojiangLabel);
				List<TextView> txtViews = new ArrayList<TextView>();
				for (int i = 0; i < MyCommon.QueryLength; i++) {
					tvYaojiang = createNum(yaojiangNums[yaojiangCount], i);
					txtViews.add(tvYaojiang);
					if (MyCommon.QueryName.equals(MyCommon.FCSHUANGSEQIU)
							&& yaojiangCount == (yaojiangNums.length - 1)
							|| MyCommon.QueryName.equals(MyCommon.FC3D)
							|| MyCommon.QueryName.equals(MyCommon.FCQICAILE)
							&& yaojiangCount == (yaojiangNums.length - 1)
							|| MyCommon.QueryName.equals(MyCommon.TCDALETOU)
							&& yaojiangCount >= (yaojiangNums.length - 2)) {
						tvYaojiang
								.setBackgroundResource(R.drawable.ball_blue32x32);
						tvYaojiang.setTextColor(getResources().getColor(
								R.color.solid_yellow));
					} else {
						tvYaojiang
								.setBackgroundResource(R.drawable.ball_red_32x32);
					}
					yaojiangCount++;
					ll2.addView(tvYaojiang);
				}
				for (int i = 0; i < MyCommon.QueryLength; i++) {
					AnimationSet mySet = new AnimationSet(true);
					mySet.setStartOffset(i * 1000);
					mySet.addAnimation(AnimationHelper.show(1000));
					mySet.addAnimation(AnimationHelper.rotateSelf(1000, 720));
					if (i == MyCommon.QueryLength - 1) {
						mySet.setAnimationListener(new AnimationListener() {
							public void onAnimationEnd(Animation animation) {
								AnimationSet set = new AnimationSet(true);
								set.addAnimation(AnimationHelper.show(1000));
								set.addAnimation(AnimationHelper.scaleOut(1000));
								set.setFillAfter(true);
								btnYaojiang.setAnimation(set);
								btnYaojiang.setEnabled(true);
							}

							public void onAnimationRepeat(Animation animation) {
							}

							public void onAnimationStart(Animation animation) {
							}

						});
					}
					txtViews.get(i).startAnimation(mySet);
				}

				ll.addView(ll2);
			}
		});

		ll.addView(btnYaojiang);
		SharedPreferences sp =this.getSharedPreferences("ad",MODE_PRIVATE);
		if (!"test".equals(FileHelper
				.getText("http://android.snbw.cn/test.txt"))  && !"no".equals(sp.getString("has", ""))) {
			ll.addView(ViewHelper.getAdView(this));
		}
		setContentView(sv);
	}

	private void choose() {
		progressDialog = ProgressDialog.show(this.getParent(),
				MyCommon.QueryName + MyCommon.CHOOSEACTION, "选号中……", true);
		new Thread(new Runnable() {
			public void run() {
				try {
					QueryProcess query = new QueryProcess();
					if (query.hasNewData()) {
						query.query();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				setContentByHandler();
				progressDialog.dismiss();
			}
		}).start();
	}

	private void createNums(String[] nums, View tvLabel) {
		ll1 = new LinearLayout(this);
		ll1.setLayoutParams(params);
		ll1.setOrientation(LinearLayout.HORIZONTAL);
		ll1.addView(tvLabel);
		if (MyCommon.QueryName.equals(MyCommon.TCDALETOU)
				&& nums[nums.length - 2] == nums[nums.length - 1]) {
			int temp = Integer.parseInt(nums[nums.length - 1]);
			nums[nums.length - 1] = temp - 2 > 0 ? String.valueOf(temp - 2)
					: String.valueOf(temp + 2);
		}
		for (int i = 0; i < nums.length; i++) {
			tvNum = createNum(nums[i], i);
			if (MyCommon.QueryName.equals(MyCommon.FCSHUANGSEQIU)
					&& i == (nums.length - 1)
					|| MyCommon.QueryName.equals(MyCommon.FC3D)
					|| MyCommon.QueryName.equals(MyCommon.FCQICAILE)
					&& i == (nums.length - 1)
					|| MyCommon.QueryName.equals(MyCommon.TCDALETOU)
					&& i >= (nums.length - 2)) {
				tvNum.setBackgroundResource(R.drawable.ball_blue32x32);
				tvNum.setTextColor(getResources()
						.getColor(R.color.solid_yellow));
			} else {
				tvNum.setBackgroundResource(R.drawable.ball_red_32x32);
			}

			ll1.addView(tvNum);
		}
		ll.addView(ll1);
	}

	private TextView createNum(String num, int index) {
		if (MyCommon.QueryName.equals(MyCommon.FCSHUANGSEQIU)
				&& Integer.parseInt(num) > 16
				&& index == MyCommon.QueryLength - 1) {
			num = String.valueOf(Integer.parseInt(num) / 2);
		}
		if (MyCommon.QueryName.equals(MyCommon.TCDALETOU)
				&& Integer.parseInt(num) > 12
				&& index >= MyCommon.QueryLength - 2) {
			num = String.valueOf(Integer.parseInt(num) / 2 - 5);
		}
		TextView result = new TextView(ChooseActivity.this);
		result.setLayoutParams(paramsNum);
		result.setTextSize(22);
		result.setGravity(Gravity.CENTER);
		result.setText(num);
		return result;

	}
}
